package dao;

import bean.ChatGroup;
import bean.Message;
import bean.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class ChatGroupDao {



    /**
     * 建群
     * @param chatGroup
     * @return
     */
    public boolean creatGroup(ChatGroup chatGroup){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int num = 0;
        try {
// 获取数据库连接
            conn = JDBCUtils.getConnection();
// 根据sql 模板创建对应的预编译对象并将传入的形参赋值模板，替换掉sql 模板中“？”
            String sql = "insert into chat.chatgroup(id, name, img, master, remarks, delFlag) values (?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, chatGroup.getChatGroupId());
            pstmt.setString(2, chatGroup.getName());
            pstmt.setString(3, chatGroup.getImg());
            pstmt.setString(4, chatGroup.getMaster());
            pstmt.setString(5, chatGroup.getRemarks());
            pstmt.setString(6, chatGroup.getDelFlag());
            num = pstmt.executeUpdate();
            if (num > 0) {
                return true;
            }
        } catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block

            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.release(pstmt, conn);
            } catch (SQLException e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 加人
     * @param group
     * @param user
     * @return
     */
    public boolean addMember(ChatGroup group, User user){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
// 获取数据库连接
            conn = JDBCUtils.getConnection();
// 根据sql 模板创建对应的预编译对象并将传入的形参赋值模板，替换掉sql 模板中“？”
            String sql = "insert into chat.chatgroupuser (chatGroupID, userId) values (?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, group.getChatGroupId());
            pstmt.setString(2, user.getId());


// 执行插入数据操作
            int num = pstmt.executeUpdate();
            if (num > 0) {
                return true;
            }
        } catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block

            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.release(pstmt, conn);
            } catch (SQLException e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 列出所有成员
     * @param group
     * @return
     */
    public List<User> listMember(ChatGroup group){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs  = null;
        List<User> list = new ArrayList<>();
        UserDao userDao = new UserDao();

        try {
// 获取数据库连接
            conn = JDBCUtils.getConnection();
// 根据sql 模板创建对应的预编译对象并将传入的形参赋值模板，替换掉sql 模板中“？”
            String sql = "select userId from chat.chatgroupuser where chatGroupID="+group.getChatGroupId();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                String id = rs.getString("userId");
                list.add(userDao.findUser(id));
            }
// 执行插入数据操作
        } catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block

            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.release(pstmt, conn);
            } catch (SQLException e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 踢人
     * @param group
     * @param user
     * @return
     */
    public boolean kickPeople(ChatGroup group, User user){
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement("delete from chat.chatgroupuser where userId='"+user.getId()+"'and chatGroupID="+group.getChatGroupId());
            int num = ps.executeUpdate();
            if (num > 0) {
                return true;
            }
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    public ChatGroup findGroup(String chatGroupID){
        try {
            Connection conn = JDBCUtils.getConnection();
            String sql = "select * from chat.chatgroup where id =" + chatGroupID;
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                ChatGroup chatGroup =  new ChatGroup(
                        rs.getString("id"),
                        rs.getString("master")
                );
                chatGroup.setImg(rs.getString("img"));
                chatGroup.setName(rs.getString("name"));
                chatGroup.setRemarks(rs.getString("remarks"));
                return  chatGroup;
            }


        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


}
